Passed
Pull Request — master (#148)
by Mathieu
03:30
created

GetFairCalendarOverview   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 42
dl 0
loc 52
c 0
b 0
f 0
rs 10

2 Functions

Rating   Name   Duplication   Size   Complexity  
A index 0 29 3
A getNumberOfMealTickets 0 19 5
1
import { EventType } from './Event.entity';
2
import { ICalendar } from './ICalendar';
3
import { ICalendarOverview } from './ICalendarOverview';
4
5
export class GetFairCalendarOverview {
6
  public index(items: ICalendar[]): ICalendarOverview {
7
    const itemsByDate = [];
8
    const overview: ICalendarOverview = {
9
      mission: 0,
10
      dojo: 0,
11
      formationConference: 0,
12
      leave: 0,
13
      support: 0,
14
      other: 0,
15
      mealTicket: 0
16
    };
17
18
    for (const item of items) {
19
      const dayIndex = new Date(item.getDate()).getDate() - 1;
20
      const time = item.getTime() / 100;
21
      const type = item.getType();
22
23
      if (itemsByDate[dayIndex]) {
24
        itemsByDate[dayIndex].push({time, type});
25
      } else {
26
        itemsByDate[dayIndex] = [{time, type}];
27
      }
28
29
      overview[item.getType()] += time;
30
    }
31
32
    return {
33
      ...overview,
34
      mealTicket: this.getNumberOfMealTickets(Object.values(itemsByDate))
35
    };
36
  }
37
38
  public getNumberOfMealTickets(itemsByDate: any[]): number {
39
    let mealTicket = 0;
40
41
    for (const sortedEvent of itemsByDate) {
42
      let totalPerDay = 0;
43
44
      for (const { time, type } of sortedEvent) {
45
        if (type !== EventType.OTHER) {
46
          totalPerDay += time;
47
        }
48
      }
49
50
      if (totalPerDay > 0.5) {
51
        mealTicket++;
52
      }
53
    }
54
55
    return mealTicket;
56
  }
57
}
58